<template>
	<div class="order">
		<!-- 地址 -->
		<div class="address" v-if="address.name" @click="toAddressList">
	      	<div class="item">
	        	<div class="list">
		          	<div class="addresslist">
		            	<div>
		             	 	<span>{{address.name}}</span>
		              		<div class="moren">默认</div>
		            	</div>
		            	<div class="info">
			              	<p>{{address.mobile}}</p>
			              	<p>{{address.address+address.address_detail}}</p>
			            </div>
		            	<div></div>
		          	</div>
	        	</div>
	      	</div>
	    </div>

	    <!-- 默认地址 -->
	    <div class="seladdress" v-else @click="toAdd">请选择默认地址</div>

		<!-- 订单信息 -->
	    <div class="orderbox">
	      	<div class="item" >
		        <div>商品合计</div>
		        <div>¥{{allprice}}</div>
	      	</div>
	      	<div class="item">
		        <div>运费</div>
		        <div>免运费</div>
	      	</div>
	      	<div class="item">
		        <div>优惠券</div>
		        <div>暂无</div>
	      	</div>
	    </div>

		<!-- 购买商品的信息 -->
	    <div class="cartlist">
      		<div class="item" v-for="(item,index) in listData" :key="index">
        		<div class="con">
          			<div class="left">
            			<div class="img">
              				<img :src="item.list_pic_url" alt="">
            			</div>
            			<div class="info">
              				<p>{{item.goods_name}}</p>
              				<p>¥{{item.retail_price}}</p>
            			</div>
          			</div>
          			<div class="right">
          				<!-- 购买个数 -->
            			<div class="num">x{{item.number}}</div>
          			</div>
       			</div>
      		</div>
    	</div>

    	<!-- 支付 -->
    	<div class="bottom">
    		<!-- 购买总金额 -->
	      	<div>实付：¥{{allprice}}</div>
	      	<div class="pay" @click="toPay">支付</div>
	    </div>
	</div>
</template>

<script type="text/javascript">
import {get,post} from '@/util'
import {formatTime,onlyTime} from '../../utils/index.js'
export default{
	data(){
		return{
			address: {},
	      	allprice: '',
	      	openId: '',
	      	addressId: '',
	      	listData: [],
	      	time:'',
	      	number:'',
	      	goodsId:'',
		}
	},
	onShow(){
		const userinfo = wx.getStorageSync('userinfo')
		this.only_id = wx.getStorageSync('id')
		this.openId = userinfo.openId
		if(this.$root.$mp.query.goods_id){
			console.log("从立即购买来")
			this.goodsId = this.$root.$mp.query.goods_id
			this.number = this.$root.$mp.query.number
			this.getBuyDetail()
		}else{
			this.getDetail()
			console.log("从购物车来")
		}
	},
	methods: {
		toAddressList(){
			wx.navigateTo({
	        	url: '/pages/addressSelect/main'
	      	})
		},
		toAdd(){
			wx.navigateTo({
        		url: '/pages/addressSelect/main'
      		})
		},
		// 立即购买info
		async getBuyDetail(){
			const data = await get('/app/order/buyAction',{
				openId:this.openId,
				goodsId:this.goodsId,
				addressId:this.addressId
			})
			if(data){
				this.listData = data.buyData
				this.address = data.address
			}
			this.listData.map((item) =>{
				this.allprice = Number(item.retail_price * this.number)
			})
		},
		//购物车页面获取订单信息
		async getDetail(){
			const data = await get('/app/order/detailAction',{
				openId:this.openId,
				addressId:this.addressId
			})
			if(data){
				this.listData = data.goodsList
				this.address = data.address
				this.allprice = data.allprice
				// console.log("cart支付页面商品信息:",this.listData)
			}
		},
		toPay () {
			var that =this
	      	wx.showModal({
          		title: '确定要付款？',
                showCancel:true,
                mask:true,
                cancelText:'取消支付',
                confirmText:'确定支付',
          		success: (res) => {
          			if(res.confirm){
          				wx.showLoading({
          					title:'支付中...',
         					mask:true,
          				})
          				setTimeout(function(){

					      	that.pay()
					    },1500);
          				
          			}		
		        }    
	        }); 
	    },
	    async pay (){
	    	var only_time =	onlyTime(new Date())
	    	var time =	formatTime(new Date())
	    	var only_order=this.only_id+only_time
	    	var address =this.address.name+' '+this.address.mobile+' '+this.address.address+this.address.address_detail
	    	const orderDetail = await post('/app/order/orderDetail',{
	    		openId:this.openId,
	    		orderId:only_order,
	    		time:time,
	    		goods:this.listData,
	    		allprice:this.allprice,
	    		address:address,
	    	})
	    	// 支付成功
	    	if(orderDetail){
	    		var that =this
	    		wx.hideLoading()
	    		wx.navigateTo({
		        	url: '/pages/myFootprint/main'
		      	})
		      	if(!that.goodsId){ 
		      		that.delCart()		 
				}
	    		// wx.showToast({
	    		// 	title: '支付成功',//提示文字
			    //      duration:1500,//显示时长
			    //      mask:true,
			    //      icon:'success', 
			    //      success:function(){
			    //      	if(that.goodsId){
				   //       	setTimeout(function(){
						 //      	wx.navigateBack({
				   //        			delta:1
				   //      		});
						 //    },1500);   
	    		// 		}
	    		// 		else{	   
	    		// 			that.delCart()			
				   //       	setTimeout(function(){
						 //      	wx.switchTab({
				   //        			url: '/pages/cart/main'
				   //      		});
						 //    },1500);   
	    		// 		}    	
			    //      },
	    		// })
	    	}
	    },
	    async delCart(){
	    	console.log("zhixindel")
	    	let newgoodsid = []
      		for (let i = 0; i < this.listData.length; i++) {
        		const goods_id = this.listData[i].goods_id
          		newgoodsid.push(goods_id)
      		}
	    	let goodsId = newgoodsid.join(',')
	    	// 删除选中的购物车信息
      		const data = await post('/app/cart/delCart',{
      			goodsId: goodsId,
      			openId: this.openId
      		})
	    },
	}
}
</script>

<style lang="scss">
	@import './order.scss';
</style>